package com.elderly.controller;

import com.elderly.config.DFSConfig;
import com.elderly.model.result.RequestResult;
import com.elderly.model.result.ResultCode;
import com.elderly.utils.GenerationCode;
import com.elderly.utils.PhoneUtils;
import com.elderly.utils.RedisUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Key;
import java.security.cert.TrustAnchor;

/**
 * @Description 验证码发送
 * @Date 2021/10/5 17:06
 * @Created 30500
 */
@RestController
@RequestMapping("code")
public class ShortCodeController {
    /**
     * redis连接工具
     */
    @Resource
    private PhoneUtils phoneUtils;
    /**
     * @description 获取验证码
     * @author 30500
     * @date 2021/10/5 17:10
     * @type [java.lang.String 手机号]
     * @return com.elderly.model.result.RequestResult
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    public RequestResult getCode(@RequestParam(value = "phone", required = true) String phone) {
        // 尝试获取验证码
        String code = phoneUtils.getCode(phone);
        if (code == null) {
            // 生成验证码
            code = GenerationCode.generationCode();
            // 验证码存储
            if (phoneUtils.setCode(phone, code)) {
                return new RequestResult(code);
            } else {
                return new RequestResult(ResultCode.RC_503, "服务器忙碌");
            }
        }
        return new RequestResult(code);
    }
}
